package com.sys.dao.impl;

import java.io.File;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.sys.constant.UploadAndDownloadPath;
import com.sys.dao.FileDao;
import com.sys.dao.UploadDao;
import com.sys.domain.Upload;
import com.sys.service.UploadOperationSer;
import com.sys.service.impl.UploadOperationImpl;
import com.sys.utils.C3P0Utils;

public class UploadDaoImpl implements UploadDao {

	private QueryRunner queryRunner;
	public UploadDaoImpl() {
		queryRunner = new QueryRunner(C3P0Utils.getDataSource());
	}
	
	@Override
	public Boolean addUpload(Upload upload) throws SQLException {
		String sql = "insert into upload values(?, ?, ?, ?, ?)";
		int row = queryRunner.update(sql, upload.getFileId(), upload.getUserId(), upload.getUpTime(), upload.getStatus(), upload.getSize());
		return row > 0? true:false;
	}

	@Override
	public List<Upload> findAllSelfUpload(String userId) throws SQLException {
		String sql = "select file_id as fileId, user_id as userId, upload_time as upTime, status as status from upload where user_id=?";
		List<Upload> uoloadRecord = queryRunner.query(sql, new BeanListHandler<Upload>(Upload.class), userId);
		if(uoloadRecord.size()>0)
			Collections.sort(uoloadRecord);
		return uoloadRecord;
	}

	@Override
	public Boolean deleteRecord(String fileId) throws SQLException {
		System.out.println(fileId);
		String sql = "delete from upload where file_id=?";
		int row = queryRunner.update(sql, fileId);
		System.out.println("row:   " + row);
		File file = new File(UploadAndDownloadPath.path+"\\"+fileId);
		boolean delete = true;
		if(file.exists())
			delete = file.delete();
		System.out.println(delete);
		FileDao fDao = new FileDaoImpl();
		if(fDao.isFileExist(fileId)) {
			fDao.deleteFile(fileId);
		}
		return row>0&&delete? true:false;
	}

	@Override
	public Boolean isFileHasUpload(String fileName) throws SQLException {
		String sql = "select * from upload where file_id=?";
		Upload upload = queryRunner.query(sql, new BeanHandler<Upload>(Upload.class), fileName);
		return upload != null?true:false;
	}

	@Override
	public List<Upload> findAllNotAppro() throws SQLException {
		
		String sql = "select file_id as fileId, user_id as userId, upload_time as upTime, size as size from upload where status = 0";
		List<Upload> notAppro = queryRunner.query(sql, new BeanListHandler<Upload>(Upload.class));
		UploadOperationSer uOS = new UploadOperationImpl();
		for(Upload eUpload: notAppro) {
			eUpload.setFileId(uOS.deleteUserIdOfFile(eUpload.getFileId()));
		}
		if(notAppro.size() > 0)
			Collections.sort(notAppro);
		return notAppro;
	}

	@Override
	public Boolean changeStatus(String fileId, String status) throws SQLException {
		String sql = "update upload set status = ? where file_id = ?";
		int row = queryRunner.update(sql, status, fileId);
		return row>0?true:false;
	}

	@Override
	public Upload getAllInfoByFileId(String fileId) throws SQLException {
		String sql = "select file_id as fileId, user_id as userId, upload_time as upTime, size as size from upload where file_id = ?";
		Upload upload = queryRunner.query(sql, new BeanHandler<Upload>(Upload.class), fileId);
		return upload;
	}
}
